<template>
	<view class="info">
		<view class="message_top">
			<view class="message_top_title" :style="{top:computedTop}">个人信息</view>
			<view class="message_top_back" :style="{top:computedTop}" @click="Back">
				<image src="../../static/search/back.png" class="bfbimages"></image>
			</view>
		</view>

		<view class="info_box">
			<view>
				<text>头像</text>
				<view style="display: flex;align-items: center;justify-content: flex-end;">
					<view style="width: 72rpx;height: 72rpx;border-radius: 50%;overflow: hidden;margin-right: 10rpx;"
						class="flex">
						<image :src="Info.avatar" class="bfbimages" @click="Upimages"></image>
					</view>
					<view style="width: 16rpx;height: 24rpx;text-align: center;line-height: 12rpx;" class="flex">
						<image src="../../static/hei_right.png" class="bfbimages"></image>
					</view>
				</view>
			</view>

			<view>
				<text>姓名</text>
				<input type="text" style="font-size: 28rpx;color: #666666;text-align: right;font-weight: 500;"
					v-model="Info.nickname" />
			</view>

			<view>
				<text>ID</text>
				<input type="text" style="font-size: 28rpx;color: #666666;text-align: right;font-weight: 500;"
					v-model="Info.id" :disabled="true" />
			</view>

			<view>
				<text>手机号</text>
				<input type="text" style="font-size: 28rpx;color: #666666;text-align: right;font-weight: 500;"
					v-model="Info.phone" :disabled="true" />
			</view>

			<view>
				<text>信用积分</text>
				<input type="text" style="font-size: 28rpx;color: #666666;text-align: right;font-weight: 500;"
					v-model="Info.credit_nums" :disabled="true" />
			</view>
		</view>
		<view class="info_btn" @click='Cbaoc'>确定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				menuButtonInfo: null,
				name: '李海洋',
				id: 659874123,
				phone: 17629933391,
				jf: '3587',
				Info: ''
			}
		},
		computed: {
			computedTop() {
				if (this.menuButtonInfo) {
					return `${this.menuButtonInfo.height * 2 - 6}px`;
				}
				return '0px';
			},
		},
		onLoad() {
			const info = uni.getMenuButtonBoundingClientRect();
			this.menuButtonInfo = info;
		},
		onShow() {
			this.InfoApi()
		},
		methods: {
			Cbaoc() {
				let data = {
					avatar: this.Info.avatar,
					nickname: this.Info.nickname
				}
				const token = uni.getStorageSync('token'); // 请根据实际情况获取 token
				uni.request({
					url: this.$uts + '/api/user/updateUser',
					method: 'POST',
					header: {
						'Authorization': `${'Bearer'} ${token}`,
						'server': 1,
						'token': uni.getStorageSync('token')
					},
					data,
					success: res => {
						console.log(res.data.data, '修改个人信息');
						if (res.data.code == 1) {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 1800)
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return false
						}
					}
				})
			},
			Upimages() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: this.$uts + '/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								token: uni.getStorageSync('token'),
								type: 2
							},
							header: {
								'token': uni.getStorageSync('token'),
								'server': 1,
							},
							success: (res) => {
								let Ima = JSON.parse(res.data)
								console.log(Ima.data.url, 'Ima-Ima-Ima');
								this.Info.avatar = Ima.data.url
							}
						});
					}
				});
			},
			InfoApi() {
				const token = uni.getStorageSync('token'); // 请根据实际情况获取 token
				uni.request({
					url: this.$uts + '/api/user/getUser',
					method: 'POST',
					header: {
						'Authorization': `${'Bearer'} ${token}`,
						'server': 1,
						'token': uni.getStorageSync('token')
					},
					success: res => {
						console.log(res.data.data, '个人信息');
						this.Info = res.data.data
					}
				})
			},
			Back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.info {
		width: 100vw;
		height: 100vh;
		float: left;
		background-color: #F6F8FB;
	}

	.message_top {
		width: 100%;
		height: 200rpx;
		position: relative;

		.message_top_title {
			width: 100%;
			text-align: center;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #333333;
			position: absolute;
			left: 0;
			z-index: 2;
		}

		.message_top_back {
			width: 44rpx;
			height: 44rpx;
			position: absolute;
			left: 30rpx;
			z-index: 9;
		}
	}

	.info_box {
		width: 650rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin: 20rpx auto 0;
		padding-left: 30rpx;
		padding-right: 30rpx;
	}

	.info_box>view {
		width: 100%;
		height: 152rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 32rpx;
		color: #333333;
	}

	.info_btn {
		width: 710rpx;
		height: 104rpx;
		background: linear-gradient(270deg, #FE4624 0%, #FF8143 100%);
		border-radius: 50rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 36rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 104rpx;
		margin: 30rpx auto 0;
	}
</style>